import axios from 'axios'
import qs from 'qs'

axios.defaults.baseURL = process.env.gateway

axios.interceptors.response.use((res) => {
  return new Promise((resolve, reject) => {
    let data = res.data
    if (res.status != 200) {
      reject(data)
    } else if (data.MSDIG == 'E') {
      reject(data.MESSAGE)
    } else {
      resolve(data)
    }
  })
})

const http = {
  get: function (url, data) {
    return axios.get(url, {
      params: data
    })
  },
  post: function (url, data) {
    return axios.post(url, qs.stringify(data))
  }
}

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }).then(response => {
      resolve(response)
    }).catch(err => {
      reject(err)
    })
  })
}

export default {
  ...http, ...{
    install: function () {
    }
  }
}
